function HandleLowMemory( event ) 
  print( "memory warning received!" ) ;
end 

system.activate( "multitouch" );

local physics = require "physics"
physics.start();
physics.setGravity( 0, 0 );
physics.setPositionIterations(12)
--physics.setVelocityIterations( 12 )
--physics.setDrawMode( "debug" ) -- shows collision engine outlines only
physics.setDrawMode( "hybrid" ) -- overlays collision outlines on normal Corona objects
--physics.setDrawMode( "normal" ) -- the default Corona renderer, with no collision outli-nes

----------------------------------------------------------------------
-- Global val
----------------------------------------------------------------------
local gameScreen;
local mainMenuScreen;

local touchInfos = {}
for i=0,5 do
	local info = {};
	info.free = true;
	info.id = 0;
	info.beganTime = 0;
	info.endedTime = 0;
	touchInfos[i] = info;
end
	
local fps = 30;
----------------------------------------------------------------------
-- End of Global val
----------------------------------------------------------------------

----------------------------------------------------------------------
-- Init Game
----------------------------------------------------------------------
function InitGame()
	mainMenuScreen = display.newGroup()	;
	gameScreen = display.newGroup()	;
	
	gameScreen.layer1 = display.newGroup();
	gameScreen.layer2 = display.newGroup();
	gameScreen.layer3 = display.newGroup();
	gameScreen.layer4 = display.newGroup();
	gameScreen.layer5 = display.newGroup();	
	gameScreen.hud = display.newGroup()	;	
	
	gameScreen:insert(gameScreen.layer1)
	gameScreen:insert(gameScreen.layer2)
	gameScreen:insert(gameScreen.layer3)
	gameScreen:insert(gameScreen.layer4)
	gameScreen:insert(gameScreen.layer5)
	gameScreen:insert(gameScreen.hud)
	
	-- Temp
	InitLevel();
end

function InitLevel()
	local bkg = display.newImage( "Resource/bkg.jpg", 0, 0 );	
	gameScreen.layer1:insert(bkg)
end
----------------------------------------------------------------------
-- GameLoop Function
----------------------------------------------------------------------

function GameLoop( event )   		
	
	--update car
	local carGroup = gameScreen.layer3;
	for i = 1, carGroup.numChildren do		
		carGroup[i]:update()
	end
	
end 

----------------------------------------------------------------------
-- Misc Function
----------------------------------------------------------------------
function GetTouchInfo(id)
	for i=0,5 do
		if touchInfos[i].id==id then
			return touchInfos[i]
		end
	end	
end

----------------------------------------------------------------------
-- Car 
----------------------------------------------------------------------
function newCar(params)
	local car;	
	-------------------------------------------------------------------
	-- CAR4
	-------------------------------------------------------------------
	if( params.type == "car4") then
		car = display.newImage( "Resource/car4.png",params.x,params.y );		
		local car4CollisionFilter = { categoryBits = 1, maskBits = 1 } 
		physics.addBody( car, { density = 1, friction = 0.3, bounce = 0.2, filter=car4CollisionFilter } )
		
		car.angularDamping = 3
		car.linearDamping = 3
		-- public func				
	end		
	
	car:addEventListener("touch", car );
	
	car.vel = params.vel;
	car.type = params.type;
	car.rotation = params.rotation;
	params.group:insert(car);	
	
	car.pushTouchId = 0
	function car:update()
		local sinx = math.sin (car.rotation*math.pi/180)
		local cosx = math.cos (car.rotation*math.pi/180)
		
		local vx = cosx*car.vel
		local vy = sinx*car.vel		
		local dx = vx/fps
		local dy = vy/fps
		local f = 5
		car:applyForce(  dx*f, dy*f, car.x, car.y )	
	end
	
	function car:push(fx,fy, posx,posy,touchId)
		car.pushTouchId = touchId
		car:applyForce( fx,fy , posx, posy )	
	end
	
	function car:touch(event) 
		if event.phase=="began" then
			--print("Car:began..occurred") 
		end
		
		if event.phase=="moved" then
			--print("Car:moving..occurred")
			if car.pushTouchId ~= event.id then
				local info = GetTouchInfo(event.id)
				local dtime = event.time-info.beganTime
				print(dtime)
				if dtime<100 then
					local dx = event.x-event.xStart;
					local dy = event.y-event.yStart;
					car:push(dx,dy,event.x,event.y,event.id)
				end
			end
		end
		
		if event.phase == "ended" or event.phase == "cancelled" then
			--print("Car:end..occurred") 
		end
	end
	
	function car:onTouch(event) 
		--if(car.touchStatus
	end
	
	return car;
end

----------------------------------------------------------------------
-- handleTouch Function
----------------------------------------------------------------------

function HandleTouch( event )  	
	
	if event.phase=="began" then
		--print("Car:began..occurred") 
		for i=0,5 do
			if touchInfos[i].free==true then
				print("Trace start")
				touchInfos[i].free = false
				touchInfos[i].id = event.id
				touchInfos[i].beganTime = event.time
				
				break
			end
		end	
	end
		
	if event.phase=="moved" then
		--print("Car:moving..occurred")
	end
	
	if event.phase == "ended" or event.phase == "cancelled" then
		for i=0,5 do
			if touchInfos[i].id==event.id then
				print("Trace end")
				touchInfos[i].free = true
				touchInfos[i].endedTime = event.time
			end
		end	
	end		
	
end 

Runtime:addEventListener( "memoryWarning", HandleLowMemory );
Runtime:addEventListener( "enterFrame", GameLoop );
Runtime:addEventListener( "touch", HandleTouch )
InitGame();

test = newCar{ type="car4",
			  x= 50,
			  y= 100,
			  group = gameScreen.layer3,
			  vel = 25,
			  rotation = 0}

--timer.performWithDelay(math.random (3000,6000), spawnBird )